package cc.telecomdigital.tdstock.activity.groups;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TabHost;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.Framework.ActivityGroupHost_Framework;
import cc.telecomdigital.tdstock.Framework.ActivityGroup_Framework;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.monitor.PortfolioActivity;
import cc.telecomdigital.tdstock.activity.groups.more.MoreActivity;
import cc.telecomdigital.tdstock.activity.groups.more.MoreSettingActivity;
import cc.telecomdigital.tdstock.activity.groups.more.push.client.Constants;

/**
 * Groups the archive activities, so they can stay within a tab of TabActivity
 * @author haibo
 *
 */
public class MoreGroup extends  ActivityGroupHost_Framework
{
	private final static String	TAG		= MoreGroup.class.getSimpleName();
	public static MoreGroup		group;
	// private ArrayList<View> history;
	public static TabHost		tabHost	= null;
	private static ActivityGroupController groupController = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//this.history = new ArrayList<View>();
		group = this;
		//set the default activity when enter the group
		/*View view = getLocalActivityManager().startActivity(
				MoreActivity.class.getSimpleName(),
				new Intent(this, MoreActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION))
				.getDecorView();
		setContentView(view);*/
		TDStockLog.d(TAG, "Create new instance  on  MoreGroup!");
	}

	public static void forceToGoToRelatingTab()
	{
		if (tabHost != null)
		{
			tabHost.setCurrentTab(4); //force to go to tab 4
		}
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		TDStockLog.d(TAG, String.format("onResume for MoreGroup and current activity is %s",
								this.getCurrentActivity().getClass().getSimpleName()));

		if (this.getCurrentActivity() instanceof PortfolioActivity)
		{
			/*View view = getLocalActivityManager().startActivity(
					MoreActivity.class.getSimpleName(),
					new Intent(this, MoreActivity.class)
							.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			setContentView(view); //clear the history(top20 stock), and force to forward to more menu list
*/	
		    TDStockLog.d(TAG, "ready to forwarding to moreActivity!");
			MoreGroup.GetActivityGroupHostControl().ActivityForwardSwitch(MoreGroup.group.getCurrentActivity() , MoreActivity.class, new Intent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|  Intent.FLAG_ACTIVITY_NO_USER_ACTION));
		}

	}

	@Override
	public void onBackPressed()
	{
		//super.onBackPressed();
		getCurrentActivity().onBackPressed(); //do  back pressed action in BaseActivity.  need to  go back to the history page directly?
	}

	@Override
	public ActivityGroupController CreateActivityGroupHostController() {
//		if (groupController != null)
//			groupController.DestoryController();

		if (groupController == null)
			groupController = new ActivityGroupController(this);
		else
			groupController.UpdateGroupHost(this);
		return groupController;
	}

	@Override
	public Class<? extends ActivityGroup_Framework> GetPrimaryActivity() {
		return MoreActivity.class;
	}

	/** For External Call **/
	public static ActivityGroupController GetActivityGroupHostControl()
	{
		return groupController;
	}
	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		TDStockLog.d(TAG, String.format("requestCode=%d,resultCode = %d", requestCode,  resultCode));
		
		if (resultCode == RESULT_OK)
		{
			Uri uri = data
					.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			if (uri != null)
			{
				String ringTonePath = uri.toString();
				TDStockLog.d(TAG, ringTonePath);
				SharedPreferences _prefse = getSharedPreferences(
						Constants.SHARED_PREFERENCE_NAME,
						Context.MODE_PRIVATE);

				_prefse.edit().putString(
						CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG,
						ringTonePath).commit();

			}
			else
			{
				TDStockLog.d(TAG, "null");
				SharedPreferences _prefse = getSharedPreferences(
						Constants.SHARED_PREFERENCE_NAME,
						Context.MODE_PRIVATE);

				_prefse.edit().putString(
						CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG,
						"null").commit();
			}

			MoreGroup.GetActivityGroupHostControl().ActivityForwardSwitch(
					MoreGroup.group.getCurrentActivity(),
					MoreSettingActivity.class,
					new Intent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
							| Intent.FLAG_ACTIVITY_NO_USER_ACTION));

		}
		else if (resultCode == RESULT_CANCELED)
		{
			MoreGroup.GetActivityGroupHostControl().ActivityForwardSwitch(
					MoreGroup.group.getCurrentActivity(),
					MoreSettingActivity.class,
					new Intent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
							| Intent.FLAG_ACTIVITY_NO_USER_ACTION));
		}

		super.onActivityResult(requestCode, resultCode, data);

	}
}